/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DaveSexton.DocProject.Sandcastle.TopicManagement;
using DaveSexton.Controls;

namespace DaveSexton.DocProject.Sandcastle.ContentManagement
{
	[Serializable]
	internal sealed class XmlCommentSectionsDocument : XmlContentDocument
	{
		#region Public Properties
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate")]
		public static readonly string VirtualCommentsDocumentName = "virtual_comments_document";
		#endregion

		#region Private / Protected
		[NonSerialized]
		private readonly IXmlTopic topic;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="XmlCommentSectionsDocument" /> class.
		/// </summary>
		public XmlCommentSectionsDocument(IXmlTopic topic, IEnumerable<XmlCommentSectionContentItem> sections, IEnumerable<string> customItems)
			: base(VirtualCommentsDocumentName, customItems)
		{
			this.topic = topic;

			foreach (XmlCommentSectionContentItem section in sections)
				if (section != null)
					Items.Add(section);
		}
		#endregion

		#region Methods
		protected internal override void NotifyItemChanged(ContentItem item)
		{
			topic.NotifySectionChanged(item.Name);

			base.NotifyItemChanged(item);
		}

		/// <remarks>
		/// Creates a custom item with the <paramref name="name"/> specified by the user.
		/// </remarks>
		public override ContentItem CreateItem(string name)
		{
			XmlCommentSection section = SandcastleBuildEngineProvider.Configuration.DocumentationTags.FindSection(name);

			if (section == null)
				section = XmlCommentSection.CreateCustom(name);

			ContentItem item = CreateItem(section);
			item.IsDirty = true;

			Items.Add(item);

			IsDirty = true;

			NotifyItemChanged(item);

			return item;
		}

		private XmlCommentSectionContentItem CreateItem(ITopicSection section)
		{
			return new XmlCommentSectionContentItem(topic.CreateSection(section.Name), section, null);
		}

		public override void Load(Importer rebaseTargetDirectory, HtmlToXmlConverterCallback converter)
		{
			throw new NotSupportedException();
		}

		public override void Save()
		{
			IsDirty = false;

			foreach (XmlCommentSectionContentItem item in Items)
				item.IsDirty = false;
		}

		public override void Save(string targetPath)
		{
			throw new NotSupportedException();
		}

		public override void Commit()
		{
			foreach (XmlCommentSectionContentItem item in Items)
				item.CommitChanges();
		}

		public override void Cancel()
		{
			foreach (XmlCommentSectionContentItem item in Items)
				item.CancelChanges();
		}
		#endregion
	}
}
